require("TSLib")
require("utils")
require("search")
require("config")


home = {}
local bid = config.AppId
-- 是否安装了应用
-- local flagInstall = isInstalledApk(bid)
-- 是否在前台运行
local flagIsFont = isFrontApp(bid)
-- 执行失败重试次数
statusCount = 0
-- 用户输入的直播间id
local videoInputId = ""

options = {
    ['dim'] = 30,
    ['flag'] = false
}

-- 获取应用名称
local function getAppNameH() 
    local name = getAppName(bid)
    return name
end
-- 弹出提示
function showTips(value)
    toast(getAppNameH())
end

-- 5、判断是否存在上划查看更多指引
function gorUpMore()
    --内容已复制到剪贴板!
    local tab = {
        "f3ffff3ffffbfffffffff9ffff8ffff8ffff87fffc3fffe1ffff07fff03fff00fff807ffc01ffe00ffe00fff003ff801ff8001f8000780001c0000000000000000c0000f8000fe000ff000ff800ffc00ffe00fff00fffc0fffe0ffff0ffffcfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00ffc00ff800ff800ff000fe000fc000f8000f8000f807ff803ff801ff000fe0007f0003f0000f0000f0000f0000f0000f0000f0000f0000f0000f0000e0000e0000e0000c0000c0000c00008000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0000f0000f0001f0003f0003f0003f0003f0003f0003f0003f0003f0001f0000f0000700003000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000$上划查看更多视频$1055$20$244",
    }
    local index = addTSOcrDictEx(tab)
    --请自行更改参数
    --1: 0,0,0,0 范围坐标，请自行修改
    --2: "ABA099 , 545F67" 偏色,多组或单组.请在偏色列表中选择
    --3: 90 匹配精度 【0-100】
    local a, b = tsFindText(index, "上划查看更多视频", 142, 732, 618, 833, "ABA099 , 545F67", 90)
    -- dialog("识别到的坐标 	x:"..x.." , y:"..y)
    if a ~= -1 and b ~=-1 then 
        utils.move(x, y)
        toast("滑动完成")
    end
    -- 点击搜索按钮直接查询用户
    tap(666, 68)
    mSleep(1000)
    toast(1)
    search.searchInit(videoInputId)
end

-- 4、判断是否存在弹出的个人信息保护指引
local function showUserInfoHelp() 
    local color_table = {
        {  365,  768, 0xffa9ba},
        {  371,  762, 0xffffff},
        {  531,  791, 0xfe2c55},
        {  190,  745, 0xfe2c55},
        {  374,  764, 0xffb2c1},
    }
if multiColor(color_table,options) then 
    tap(371,762)
end
gorUpMore() 
end

-- 3、是否进入首页
function goToHome()
    -- 判断是否进入到了抖音程序,没有进入了一直等待
    --text 参数为需要找的全部文字
    local isHome = {
        {  672,  773, 0xfaf8f8},
        {  673,  868, 0xe6e6e6},
        {  670,  986, 0xe7e7e7},
        {  677,  797, 0xf2f0f0},
    }
local x, y = findMultiColorInRegionFuzzyByTable(isHome,90,602,711,706,1044)
if x ~=-1 and y ~= -1 then
    toast("进入首页了")
    retryCount = 0
    showUserInfoHelp()
else
    statusCount = statusCount + 1
    if statusCount < config.retryCount then
        toast("没有找到呢")
        -- 等待1s自动执行
        mSleep(1000)
        goToHome()
    else
        dialog("异常错误，无法进入首页") 
    end
end
end


-- 2、判断是否已经打开了
function home.isFontShow(videId)
    videoInputId = videId
    if flagIsFont == 0 then
        showTips()
        local state = runApp(bid)
        -- 应用是否启动成功
        if state == 0 then
            goToHome()
        else 
            dialog(getAppNameH() + ":启动失败")
        end
    elseif flagIsFont == 1 then
        goToHome()
    else 
        showTips()
        local result,msg = switchApp(bid)
    end
end

return home